package com.johnyang;

import org.junit.Test;

import java.io.*;

/**
 * Created by yangxu on 2017/10/31.
 */
public class Generator {

    /**
     * 前端Web所在的文件位置
     */
    public static String  STATIC_PATH="/Volumes/Development/Web/ServerRender";

    /**
     * Java项目所在的目录
     */
    public static String CURRENT_PATH="/Volumes/Development/Web/JohnYang";

    /**
     * 项目下面的静态文件所在位置
     */
    public static String SUFFIX="/src/main/webapp/WEB-INF/statics";

    /**
     * 复制静态文件
     */
    @Test
    public void CopyStaticFiles() throws Exception{


        /**
         * 源文件
         */
        File src=new File(STATIC_PATH+STATIC_SUFFIX);

        /**
         * 目标文件
         */
        File dist=new File(CURRENT_PATH+SUFFIX);

        copyFolder(src,dist);
    }

    /**
     * 清空静态资源方法
     */
    @Test
    public void Clear(){

        delAllFile(CURRENT_PATH+SUFFIX);

    }


    public static String STATIC_SUFFIX="/dist/static";


    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            //不想删除文佳夹隐藏下面
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); // 删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean delAllFile(String path) {
        boolean flag = false;

        File file = new File(path);

        if (!file.exists()) {
            return flag;
        }

        if (!file.isDirectory()) {
            return flag;
        }

        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
                flag = true;
            }
        }

        return flag;
    }


    public static void copyFolder(File src, File dest) throws IOException {

        if (src.isDirectory()) {

            if (!dest.exists()) {
                dest.mkdir();
            }

            String files[] = src.list();

            for (String file : files) {

                /**
                 * 将webpack打包的多文件中的chunk值去掉
                 */

                String newName=file;

//                if (file.lastIndexOf(".")>=0){
//
//                    String[] strs=file.split("\\.");
//
//                    newName=strs[0]+"."+strs[2];
//                }

                File srcFile = new File(src, file);

                File destFile = new File(dest, newName);

                // 递归复制
                copyFolder(srcFile, destFile);
            }
        }
        else {

            InputStream in = new FileInputStream(src);

            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;

            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();

            out.close();

        }
    }
}
